<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
</head>
<style>
    .mxnW{
        width: 80%;
        margin: 0 auto;
        margin-top: 20px;
    }
    .hidden{
        display: none;
    }
</style>

<body>
<button style="margin-top: 10px;margin-left: 70px" onclick="addTeam()" class="layui-btn">创建团队</button>
<hr>
<!--//添加团队-->
<form class="layui-form mxnW hidden" id="add" >
    <div class="layui-form-item">
        <label class="layui-form-label">团队镖旗</label>
        <div class="layui-input-block">
            <input type="file"  name="picture" class="layui-upload-file">
            <img id="setImg" style="width: 60px;height: auto" src="" alt="">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">简介</label>
        <div class="layui-input-block">
            <textarea name="info" placeholder="请输入简介内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">团队名称</label>
        <div class="layui-input-block">
            <input type="text" name="title" required  lay-verify="required" placeholder="请输入团队名称" autocomplete="off" class="layui-input">
            <button style="margin-top: 10px" class="layui-btn" lay-submit lay-filter="formDemo">立即创建</button>
        </div>
    </div>
</form>
<script src="../../public/js/jquery.min.js"></script>
<script>
    $.ajax({
        type: 'POST',
        url:"/team/search",
        data: {data:{key:'userid',value:JSON.parse(sessionStorage.user).userid}},
        success:function (data) {
            console.log(data)
            for(var i=0;i<data.result.length;i++){
                $("#teamToProject").append(
                    '<option value='+data.result[i].teamid+'>'+data.result[i].title+'</option>'
                )
                $("#teamToSelect").append(
                        '<option value='+data.result[i].teamid+'>'+data.result[i].title+'</option>'
                    )
            }
        } ,
        error:function () {
            layer.msg("系统错误")
        }
    })

</script>
<script src="./layui/layui.js"></script>
<script>
    //Demo
    var tpl={
        data:{},
        setData:function () {
            $("#setImg").attr("src",this.data.picture||"");
            $("input[name='title']").val(this.data.title||"");
            $("textarea[name='abstract']").val(this.data.abstract||"");
        },
        getData:function () {
            return {
            }
        }
    }
    if(sessionStorage.hasOwnProperty('team')){
        tpl.data=JSON.parse(sessionStorage.team);
        //显示按钮
        $("#del").toggleClass("hidden")
    }
    if(tpl.data&&tpl.data.hasOwnProperty('listid')){
        tpl.setData();
    }
    layui.use('form', function(tpl){
        var form = layui.form();
        //监听提交
        form.on('submit(formDemo)', function(data){
            if($("#setImg").attr("src")){
                data.field.picture=$("#setImg").attr("src");
            }else{
                delete data.field.picture;
            }
            data.field.userid= JSON.parse(sessionStorage.user).userid;
            if(sessionStorage.hasOwnProperty('team')){
                data.field.teamid= JSON.parse(sessionStorage.team).teamid;
                $.ajax({
                    type: 'POST',
                    url:"/team/update",
                    data: {data:data.field},
                    success:function (data) {
                        if(data.msg.type==1){
                            layer.msg(data.msg.data);
                        }
                    } ,
                    error:function () {
                        layer.msg("系统错误")
                    }
                })
            }
            else{
                $.ajax({
                    type: 'POST',
                    url:"/team/create",
                    data: {data:data.field},
                    success:function (data) {
                        if(data.msg.type==1){
                            layer.msg(data.msg.data);
                        }
                    } ,
                    error:function () {
                        layer.msg("系统错误")
                    }
                })
                //创建团队用户关系
                $.ajax({
                    type: 'POST',
                    url:"/userProject/create",
                    data: {data:{teamid:location.href.split("?")[1],userid:JSON.parse(sessionStorage.user).userid}},
                    success:function (data) {
                        if(data.msg.type==1){
                            layer.msg(data.msg.data);
                        }
                    } ,
                    error:function () {
                        layer.msg("系统错误")
                    }
                })
            }
            return false;
        });
    });
    layui.use('upload', function(){
        layui.upload({
            url: '/ueditor/ue?action=uploadimage'
            ,success: function(res){
                console.log(res); //上传成功返回值，必须为json格式
                if(res.state=="SUCCESS"){
                    layer.msg('上传成功')
                    $("#setImg").attr("src",location.origin+res.url)
                }else{
                    layer.msg('上传失败')
                }
            }
        });
    });
    function addTeam() {
        $("#add").toggleClass("hidden")
    }
</script>
</body>
</html>